import Dict from './Dict'

const install = function(Vue) {
  Vue.mixin({
    data() {
      if (this.$options.dicts instanceof Array) {
        const dict = { dict: {}, label: {} }
        return { dict }
      }
      return {}
    },
    created() {
      if (!this.notAutoInitDict) {
        this.initDict()
      }
    },
    methods: {
      async initDict(
        cb = () => {
          this.$nextTick(() => {
            // index.vue 的生命周期函数： this.$on('dictReady') 监听
            this.$emit('dictReady')
          })
        }
      ) {
        if (this.$options.dicts instanceof Array) {
          await new Dict(this.dict).init(this.$options.dicts, cb)
        }
      }
    }
  })
}

export default { install }
